import { game } from "../../../../script/game/control/game";
import { assetsBase, companyBase, ItemBase } from "../../../../script/game/control/Item/ItemBase";
import { assetsType, itemType } from "../../../../script/game/gameType";
import { ccs } from "../../../../script/util/services";
import { WinBase } from "../../../../script/wins/WinBase";

const {ccclass, property} = cc._decorator;
enum workType {
    "make" = 0,
    "sale" = 1,
}
//调动窗口
@ccclass
export default class NewClass extends WinBase {
    //员工操作界面
    count: number = 0; //当前的人数
    maxCount: number = 0;
    changeCount: number = 0;
    item: companyBase = null;
    type: string = "0";
    start() {
        this.item = game.playerMgr.bag.companyBag.getItem(this._args.id);
        this.type = this._args.type;
        this.maxCount = this.item.calUnworkers(); //空闲人员
        
        this.nodes.name.getComponent(cc.Label).string = "生产部";
        this.initMake();
        // this.limitCount()
        this.updateData();
        this.touchEndEventScale(this.nodes.ensureBtn, () => {
            this.item.fixMaker(this.type, this.count);
            this.updateData();
            ccs.mainEmitter.emit("upCompany");
            ccs.mainEmitter.emit("upInfo");
            this._close();
        })
       
        this.touchEndEventScale(this.nodes.make, () => {
            this.type = "make";
            this.initMake();
            this.updateData();
            this.nodes.name.getComponent(cc.Label).string = "生产部";
            
        })
        this.touchEndEventScale(this.nodes.sale, () => {
            this.type = "sale";
            this.initSale();
            this.updateData();
            this.nodes.name.getComponent(cc.Label).string = "销售部";
        })
        this.touchEndEventScale(this.nodes.add, () => {
            
            let tmp = this.count;
            this.count ++;
            if(this.count > this.maxCount ) {this.count = this.maxCount};
            this.changeCount = this.count - tmp;
            this.limitCount();
        })
        this.touchEndEventScale(this.nodes.reduce, () => {
            let tmp = this.count;
            this.count --;
            if(this.count < 0) {this.count = 0};
            this.changeCount = this.count - tmp;
            this.limitCount();
        })

    }
    initMake() {
        this.count = this.item.company.makeType.count;
        // this.maxCount += this.count;
    }
    initSale() {
        this.count = this.item.company.salesType.count;
        // this.maxCount += this.count;
        this.maxCount = this.count + this.item.calUnworkers();
    }
    onSlider(tar, i) {
        let num = tar.progress;
        let tmp = this.count;
        this.count = Math.floor(this.maxCount * num);
        this.changeCount = this.count - tmp;
        this.limitCount();
    }
    updateData() {
        let progress = this.count/ this.maxCount;
        if(progress > 1) {
            progress = 1;
        }
        this.nodes.slider.getComponent(cc.Slider).progress = progress;

        this.maxCount = this.count + this.item.calUnworkers();
        this.nodes.curCount.getComponent(cc.Label).string = ccs.gf.getOmitNum(this.count);
        this.nodes.maxWorkers.getComponent(cc.Label).string = "最大：" + ccs.gf.getOmitNum(this.maxCount);
    }
    limitCount() {
        this.nodes.curCount.getComponent(cc.Label).string = ccs.gf.getOmitNum(this.count);
        this.nodes.maxWorkers.getComponent(cc.Label).string = "最大：" + ccs.gf.getOmitNum(this.maxCount);
    }
}
